package com.apex.order.service.impl;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.apex.model.bean.Order;
import com.apex.model.bean.Product;
import com.apex.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName OrderServiceImpl
 * @Description
 * @Author ZJX
 * @Date 2025/3/5 15:48
 * @Version 1.0
 **/
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Override
    @SentinelResource(value = "createOrder")
    public Order createOrder(@RequestParam("productId") Long productId, @RequestParam("userId") Long userId) {
        Order order = new Order();
        order.setId(1L);
        // 总金额
//        product.getPrice().multiply(new BigDecimal(product.getNum()))
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("ZJX ");
        order.setAddress("尚谷");
        order.setProductList(null);

        return order;
    }

    private Product getRemote(Long productId){
//        通过订单服务远程调用获取商品服务
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instances.get(0);
        String url = serviceInstance.getHost()+serviceInstance.getPort();
        return null;
    }
}
